/**
 * 
 */
package com.nikolvas.normdb;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.nikolvas.man.Age;
import com.nikolvas.man.Sex;



/**
 * @author vnikolenko
 *
 */
public class NormFileParser {

	static final String STR_GAP = "-";
	
	public static void parse(String filePath){

		BufferedReader br = null;
		
		try {
 
			String sCurrentLine;
 			
			InputStreamReader isr = new java.io.InputStreamReader(new FileInputStream(filePath), "UTF-8");
			br = new BufferedReader(isr);
			NormData nData = null;
			if( (sCurrentLine = br.readLine()) != null ){				
				String[] s = sCurrentLine.trim().split(" ");
				Sex sex = Sex.getByLitera(s[0]);
				Age age = Age.getAgeByVal(Double.parseDouble(s[1]));
				List<String> headers = new ArrayList<String>();
				List<List<Float>> data = new ArrayList<List<Float>>();
				if( (sCurrentLine = br.readLine()) != null ){
				   int normNum = Integer.parseInt(sCurrentLine.trim());
				   int j = 0;
				   while( j < normNum && (sCurrentLine = br.readLine()) != null) {
				     headers.add(sCurrentLine.trim());
				     data.add(new ArrayList<Float>());
				     j++;
			       }
				   while( (sCurrentLine = br.readLine()) != null) {
					 String[] row = sCurrentLine.trim().split(" ");
					 for(int i = 1; i < row.length; i++){
						 float val = 0;
						 String strVal = row[i];
						 try{
						     val = Float.valueOf(strVal);
						 }catch (NumberFormatException e) {
							 if( !STR_GAP.equals(strVal) ){
								 try{
									val = parseTime(strVal); 
								 }catch (NumberFormatException ex) {
									 val = NormData.GAP;
								 }							 
							 }else{								 
								 val = NormData.GAP;
							 }
						}    
						data.get(i-1).add(val);
					 }
			       }
				}   
			    nData = new NormData(sex, age, headers, data);
			}
 
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if ( br != null )
					br.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		System.out.println("1");
	}
	
	static float parseTime(String strVal) throws NumberFormatException{
		String[] ss = strVal.trim().split(":");
		float secInMin = Float.valueOf(ss[0])*60f;
		float sec = Float.valueOf(ss[1]);
		return secInMin + sec;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		parse("12m.txt");

	}

}
